import cv2
import faceUtil
import time
import os

faceSavePath = './facelib2/'

def faceInput():
    capture = cv2.VideoCapture(0)
    while True:
        name = input('请输入姓名或者‘q’退出:')
        if (name.strip() == 'q'):
            break
        cv2.namedWindow(name, cv2.WINDOW_NORMAL)
        while True:
            flag, img = capture.read()
            if flag:
                face = faceUtil.detectOneFace(img)
                imgWithBox = img.copy()
                faceUtil.drawBox(face, imgWithBox)
                cv2.imshow(name, imgWithBox)

                key = cv2.waitKey(2)
                if key == ord('s'):
                    if len(face) == 0:
                        continue

                    (x, y, w, h) = face[0]
                    fileName = name + '-' + time.strftime('%Y%m%d%H%M%S', time.localtime()) + '.jpg'
                    cv2.imwrite(os.path.join(faceSavePath, fileName), img[y:y + h, x:x + w])
                if key == ord('q'):
                    cv2.destroyWindow(name)
                    break


if __name__ == '__main__':
    faceInput()